/*
 * Copyright (C) 2010 Imavis Srl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.imavis.avimuxer;

import com.imavis.video.parse.VideoInfo;
import com.imavis.video.parse.VideoParser;
import com.imavis.video.parse.h264.H264Parser;
import com.imavis.video.parse.mjpeg.MJpegParser;
import com.imavis.video.parse.mpeg4.Mpeg4Parser;
import java.nio.ByteBuffer;

/**
 *
 * @author Aldo Brucale
 * @author Andrea Gualano
 */
final class ParseContext {
    private static final ParseContext NO_PARSER =
            new ParseContext(new NullVideoParser(), ResolutionPolicy.notSet());
    private final ResolutionPolicy resolutionPolicy;
    private final VideoParser parser;

    public static ParseContext create(final Encoding encoding) {
        switch (encoding) {
            case MPEG4:
                return new ParseContext(new Mpeg4Parser());
            case MJPEG:
                return new ParseContext(new MJpegParser());
            case H264:
                return new ParseContext(new H264Parser());
            default:
                return NO_PARSER;
        }
    }

    private ParseContext(VideoParser parser) {
        this(parser, ResolutionPolicy.parsed());
    }

    private ParseContext(VideoParser parser, ResolutionPolicy resolutionPolicy) {
        this.parser = parser;
        this.resolutionPolicy = resolutionPolicy;
    }

    public VideoParser getParser() {
        return parser;
    }

    public ResolutionPolicy getResolution() {
        return resolutionPolicy;
    }

    private static class NullVideoParser extends VideoParser {
                @Override
                protected VideoInfo doParse(ByteBuffer bb) {
                    final VideoInfo vi = new VideoInfo();
                    vi.setKeyFrame(true);
                    return vi;
                }
    }
}
